package com.monitor.dao;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.monitor.entity.ErpCustomer;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户表 Mapper 接口
 * </p>
 *
 * @author LX
 * @since 2018-09-05
 */
public interface ErpCustomerMapper extends BaseMapper<ErpCustomer> {

    @Select("select IFNULL(SUM(cus_vehicletotal),0) FROM erp_customer WHERE cus_service_com=#{cusCode}")
    Integer selectVehicletotalCount(@Param("cusCode") Integer cusCode);

    @Select("select IFNULL(SUM(cus_t_car),0) as tcarnum,IFNULL(SUM(cus_g_car),0) as gcarnum,IFNULL(SUM(cus_d_car),0) dcarnum,IFNULL(SUM(cus_psacar),0) psacarnum,IFNULL(SUM(cus_x_car),0) as xcarnum  FROM erp_customer WHERE cus_service_com=#{cusCode}  and cus_sup_company is null")
    Map<String, BigDecimal> getCustomerCarNum(@Param("cusCode") Integer cusCode);

    @Select("select IFNULL(SUM(cus_t_car),0) as tcarnum,IFNULL(SUM(cus_g_car),0) as gcarnum,IFNULL(SUM(cus_d_car),0) dcarnum,IFNULL(SUM(cus_psacar),0) psacarnum,IFNULL(SUM(cus_x_car),0) as xcarnum  FROM erp_customer WHERE cus_service_com=#{cusCode}  and cus_sup_company is null and cus_cus_type='${type}'")
    Map<String,Object> getCustomerCarNumByType(@Param("cusCode") Integer cusCode,@Param("type")String type);

    @Select("select *  FROM erp_customer   WHERE  (cus_service_com = #{cusCode} AND cus_cus_type = '#{type}' AND cus_sup_company IS NULL)")
    List<ErpCustomer> selectCoutomerByType(@Param("cusCode") Integer cusCode,@Param("type")String type);

    @Select("select * From erp_customer c where c.id=#{id} or c.cus_sup_companys like #{company} or c.cus_service_coms like #{cus}")
    public List<ErpCustomer> selectByIdOrCompanysAndServiceComes(@Param("id")int id,@Param("company")String company,@Param("cus")String cus);
    
    @Select("select * From erp_customer c where c.cus_code in (select uf.customer_code FROM erp_use_frequency uf WHERE uf.user_id in ( select u.id from erp_user u where u.customer_id=#{id} ))")
    public List<ErpCustomer> selectByCusCode(@Param("id")int id);

    /*@Select("SELECT  id AS id,CUS_CODE AS cusCode,CUS_DJ_DATE AS cusDjDate,CUS_OPERATE_TYPE AS cusOperateType,CUS_NAME AS cusName,CUS_NICKNAME AS cusNickname,CUS_ADDRESS AS cusAddress,CUS_ZIP AS cusZip,CUS_NATURE AS cusNature,CUS_LEGAL_PERSON AS cusLegalPerson,CUS_ZC_DATE AS cusZcDate,CUS_ZC_FUNDS AS cusZcFunds,CUS_URL AS cusUrl,CUS_VEHICLETOTAL AS cusVehicletotal,CUS_PSACAR AS cusPsacar,CUS_TRUCK AS cusTruck,CUS_X_CAR AS cusXCar,CUS_D_CAR AS cusDCar,CUS_T_CAR AS cusTCar,CUS_G_CAR AS cusGCar,CUS_CUS_TYPE AS cusCusType,CUS_SUP_COMPANY AS cusSupCompany,CUS_SUP_COMPANYS AS cusSupCompanys,CUS_SERVICE_COM AS cusServiceCom,CUS_SERVICE_COMS AS cusServiceComs,CUS_CUS_AGR AS cusCusAgr,CUS_CHARGE_MANAGER AS cusChargeManager,CUS_GPS_FACILITATOR AS cusGpsFacilitator,CUS_GPS_INTERFACE AS cusGpsInterface,CUS_GPS_URL AS cusGpsUrl,END_DATE AS endDate,CUS_ROLE_TYPE AS cusRoleType,contacts AS Contacts,MATERIAL_LIFE AS materialLife," +
            "CREATE_USER AS `createUser`,CREATE_TIME AS createTime  FROM erp_customer   WHERE  (cus_sup_company = #{custId} OR cus_service_com = #{custId})")
    List<ErpCustomer> getcustOfMe(@Param("custId") Integer custId);*/


    //修改用户logo
    @Update("UPDATE erp_customer SET customer_logo = #{logo} WHERE id = #{id}")
    public  Integer updateLogo(@Param("logo")String logo,@Param("id")int id);
    
    //添加客户 客户检索
    @Select("SELECT * FROM erp_customer WHERE cus_name LIKE #{word}")
    public List<ErpCustomer> selectByCusNameLike(@Param("word")String word);
    
    @Select("SELECT * from erp_customer c where c.cus_service_com in (select c1.cus_code from erp_customer c1 where c1.id=#{custId}) OR c.id=#{custId}")
    public List<ErpCustomer> selCustomerList(@Param("custId")int custId);
}
